package com.feihong.yw189.activity.shouye;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.TypedValue;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.WebInterface.AjaxWebInterface;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.http.HttpTools;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.utils.UserLoginInfo;
import com.feihong.yw189.utils.WebViewClientRToken;
import com.feihong.yw189.view.WebView4Scroll;
import com.just.agentweb.AgentWeb;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class PhoneRechargeActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_goback)
    RelativeLayout rlGoback;
    @BindView(R.id.iv_lin)
    ImageView ivLin;
    @BindView(R.id.iv_confirm)
    TextView  tvFanli;
    @BindView(R.id.webviewframe)
    RelativeLayout webviewFrame;
    @BindView(R.id.swiperefreshlayout)
    SwipeRefreshLayout mswiperefreshlayout;
    //
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_notice);
        unbinder = ButterKnife.bind(this);

        initView();
        bindData();
    }

    @Override
    public void setTheme() {

    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {
    }

    public void initView() {
        setBackButtonFinish();
        setPageTitle("话费充值");
        ivLin.setVisibility(View.GONE);
        tvFanli.setVisibility(View.VISIBLE);
        webviewFrame.setVisibility(View.VISIBLE);
        tvFanli.setText("返利说明");


        // AgentWeb 保持了 WebView 的使用 ，
        //设置刷新时动画的颜色，可以设置4个
        mswiperefreshlayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mswiperefreshlayout.setColorSchemeResources(R.color.appRed,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        mswiperefreshlayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));

        mswiperefreshlayout.setEnabled(false);
    }

    private void bindData() {
        if (mAgentWeb == null) {
            WebView4Scroll webView4Scroll = new WebView4Scroll(getActivity(), mswiperefreshlayout);
            webView4Scroll.setVerticalScrollBarEnabled(false);
            mAgentWeb = AgentWeb.with(this)
                    .setAgentWebParent(webviewFrame, new LinearLayout.LayoutParams(-1, -1))
                    .useDefaultIndicator()
                    .setWebViewClient(mWebViewClient)
                    .setMainFrameErrorView(R.layout.agentweb_errorlayout, R.id.agentweb_re_query_data_layout)
                    .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                    .createAgentWeb()
                    .ready()
                    .go(HttpTools.PHONE_RECHARGE + "?token="+UserLoginInfo.getUserLoginInfoInstance().getUserToken()+"&version="+"and." + AppUtils.getAppVersionName());
            mAgentWeb.getAgentWebSettings().getWebSettings().setGeolocationEnabled(false);

            //屏蔽长按弹出复制按钮
         /*   mAgentWeb.getWebCreator().get().setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });*/
        } else {
            mAgentWeb.getWebCreator().getWebView().setVisibility(View.VISIBLE);
            mAgentWeb.getUrlLoader().reload();
        }

        //添加Ajax数据请求的接口
        mAgentWeb.getJsInterfaceHolder().addJavaObject(AjaxWebInterface.AJAX_KEY, new AjaxWebInterface(this, mAgentWeb.getJsAccessEntrace()));
    }

    private WebViewClient mWebViewClient = new WebViewClientRToken() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mswiperefreshlayout.setRefreshing(false);
        }
    };

    @OnClick({R.id.rl_goback,R.id.iv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_goback:
                finish();
                break;
            case R.id.iv_confirm:
                //返利说明
                Intent intent = new Intent(PhoneRechargeActivity.this, BackMoneyActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(unbinder != null){
            unbinder.unbind();
        }
        if (mAgentWeb != null) {
            mAgentWeb.destroy();
        }
    }
}